import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Star here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Star extends Actor
{
    /**
     * Act - do whatever the Star wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private int brightness;
    private boolean twinkling;
    private boolean rising = false;
    private int currentStep;
    private int distance;
    private double movementMultiplier;
    private double truex,truey;
    private boolean init = false;
    
    private int mvmnt = 1;
    
    public Star(){
        brightness = Greenfoot.getRandomNumber(254)+1;
        currentStep = brightness;
        GreenfootImage i = new GreenfootImage(2,2);
        i.setColor(new Color(brightness,brightness,brightness,brightness));
        i.drawOval(0,0,2,2);
        setImage(i);
        distance = Greenfoot.getRandomNumber(4)+1;
        movementMultiplier = 1/(double)distance;
        truex = 0;
        truey = 0;
    }
    public void act() 
    {
        if(!init){
        truex = getX();
        truey = getY();
        init = true;
        }
        if(twinkling){
        twinkleStep();
        }
        if(Greenfoot.getRandomNumber(100) == 1){
        twinkling = true;
        }
        
        
        
    }
    
    public void setMove(int x, int y){
        double a,b;
        truex += (x*movementMultiplier);
        truey += (y*movementMultiplier);
        
        
       
        super.setLocation((int)truex,(int)truey);
        
    }
    
    private void twinkleStep(){
        if(rising){
          currentStep+=3;
          if(!(currentStep<brightness)){
            rising = false;
            currentStep = brightness;
            twinkling = false;
          }
        }else{
          if(currentStep > 2){  
          currentStep-=3;
          }else{
            currentStep--;
            }
          if(currentStep <= 0){
            rising = true;
          }
        }
        
       
    
    }
    public void update(){
    if(getX()>998){
    super.setLocation(3,Greenfoot.getRandomNumber(600));
    //movementMultiplier = 1/(Greenfoot.getRandomNumber(4)+1);
    init = false;
    }
    if(getX() < 2){
    super.setLocation(997,Greenfoot.getRandomNumber(600));
    //movementMultiplier = 1/(Greenfoot.getRandomNumber(4)+1);
    init = false;
    }
    if(getY() > 598){
    super.setLocation(Greenfoot.getRandomNumber(1000), 3);
   // movementMultiplier = 1/(Greenfoot.getRandomNumber(4)+1);
    init = false;
    }
    if(getY() < 2){
    super.setLocation(Greenfoot.getRandomNumber(1000), 597);
    //movementMultiplier = 1/(Greenfoot.getRandomNumber(4)+1);
    init = false;
    }
    }
}
